package csu.web.jpetstore.web.servlet;

import csu.web.jpetstore.domain.Category;
import csu.web.jpetstore.domain.Product;
import csu.web.jpetstore.persistence.impl.LoggerDAOImpl;
import csu.web.jpetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import static csu.web.jpetstore.persistence.impl.LoggerDAOImpl.loginLogger;

public class CategoryViewServlet extends HttpServlet {

    private CatalogService catalogService;

    private static final String CATEGORY_VIEW = "/WEB-INF/jsp/catalog/category.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryId = req.getParameter("categoryId");
        catalogService = new CatalogService();
        LoggerDAOImpl loggerDAO=new LoggerDAOImpl();
        loginLogger.setStarttime();
        loginLogger.setAction(loginLogger.getUsername()+"查看"+categoryId+"类");
        try {
            loggerDAO.logger(loginLogger);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //获取商品大类
        Category category = catalogService.getCategory(categoryId);
        //获取商品列表
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        HttpSession session = req.getSession();
        if (session.getAttribute("loginAccount")==null) {

        }
        session.setAttribute("category", category);
        session.setAttribute("productList", productList);
        //实现页面跳转
        req.getRequestDispatcher(CATEGORY_VIEW).forward(req,resp);
    }
}
